package com.bmt.pos.dao;

import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import com.bmt.core.BaseDao;
import com.bmt.pos.bean.CodeTable;
import com.bmt.pos.bean.Store;

@Repository("storeDao")
public class StoreDao extends BaseDao {

	public Store addStore(final Store aStore) {
		aStore.setId(super.saveAndReturnKey(aStore).intValue());
		return aStore;
	}
	

	public List<Store> getStoreList() {
		return getJdbcTemplate().query("select *  from sys_store where active=1 ",
				new BeanPropertyRowMapper<Store>(Store.class));
	}

	public Store getStoreByColunm(String columnName, Object value) {
		return getJdbcTemplate().queryForObject("select *  from sys_store where active=1 and " + columnName + "=?",
				new BeanPropertyRowMapper<Store>(Store.class), value);
	}

	public boolean saveStore(Store store) {
		boolean flag = false;
		try {
			Number newId = saveAndReturnKey(store);
			if (newId != null) {
				flag = true;
			}
		} catch (Exception e) {
			e.printStackTrace();

		} finally {

		}
		return flag;
	}

	public List<CodeTable> getOSWList() {
		return getJdbcTemplate().query("select *  from sys_code where kind='osw' ",
				new BeanPropertyRowMapper<CodeTable>(CodeTable.class));
	}

	@Override
	public int getDataTotal() {
		return getJdbcTemplate().queryForInt("select count(*) from sys_store where active=1");
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Store> getDataByPage(int pageNum, int pageSize) {
		return getJdbcTemplate().query("select *  from sys_store where active=1 order by id limit ?, ?", new BeanPropertyRowMapper<Store>(Store.class),pageNum,pageSize);
	}
}
